package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.ProductModel;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.ui.view.FoodView;

import java.util.ArrayList;
import java.util.List;

public class MainFoodsAdapter extends RecyclerView.Adapter<MainFoodsAdapter.MyViewHolder> {

    private LayoutInflater inflater;

    private Context mContext;

    private List<ProductModel> datas = new ArrayList<>();

    private UserClick userClick;

    public UserClick getUserClick() {
        return userClick;
    }

    public void setUserClick(UserClick userClick) {
        this.userClick = userClick;
    }

    public MainFoodsAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }


    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_main_foods, parent, false);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.categoryTv.setText(datas.get(position).getClsname());
        holder.foodsView.getFoodsCardAdapter().setDatas(datas.get(position).getDeviceList());
        holder.foodsView.setOnClickListener(view -> {
            if (userClick != null) {
                userClick.userClick(datas.get(position));
            }
        });
    }

    public void setDatas(List<ProductModel> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView categoryTv;
        FoodView foodsView;

        public MyViewHolder(View itemView) {
            super(itemView);
            categoryTv = itemView.findViewById(R.id.categoryTv);
            foodsView = itemView.findViewById(R.id.foodsView);
        }
    }

    public interface UserClick {
        void userClick(ProductModel productModel);
    }
}
